<template>
	<el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
		<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
			label-width="80px">
			<el-form-item label="试卷名称" prop="title">
				<el-input v-model="dataForm.title" placeholder="试卷名称"></el-input>
			</el-form-item>
			<el-form-item label="所属课程" prop="courseId">
				<el-autocomplete class="inline-input" v-model="dataForm.courseName" :fetch-suggestions="querySearch"
					placeholder="请输入内容" @select="handleSelect"></el-autocomplete>
				<input ref="mainId" value="0" hidden="hidden"></input>
			</el-form-item>
			<el-form-item label="题目数量" prop="topicNum">
				<el-input v-model="dataForm.topicNum" placeholder="题目数量"></el-input>
			</el-form-item>
			<el-form-item label="时长(分钟)" prop="duration">
				<el-input v-model="dataForm.duration" placeholder="文章昵称"></el-input>
			</el-form-item>
			<el-form-item label="考试类型" size="mini" prop="testMode">
				<el-radio-group v-model="dataForm.testMode">
					<el-radio :label="1">练习</el-radio>
					<el-radio :label="2">考试</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="状态" size="mini" prop="status">
				<el-radio-group v-model="dataForm.status">
					<el-radio :label="0">停用</el-radio>
					<el-radio :label="1">正常</el-radio>
				</el-radio-group>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
		</span>
	</el-dialog>
</template>

<script>
	import {
		isEmail,
		isMobile
	} from '@/utils/validate'
	export default {
		data() {
			return {
				visible: false,
				restaurants: [],
				searchcuslist: [],
				searchcurriculum: '', 
				dataForm: {
					title: '',
					duration: '',
					testMode: '',
					status: 1,
					courseId: 0,
					courseName: '',
					topicNum: 0
				},
				dataRule: {
					title: [{
						required: true,
						message: '试卷名称不能为空',
						trigger: 'blur'
					}],
					duration: [{
						required: true,
						message: '时长不能为空',
						trigger: 'blur'
					}],
					courseName: [{
						required: true,
						message: '课程不能为空',
						trigger: 'blur'
					}]
				},
			}
		},
		created() {
			this.mohuSearch()
		},
		methods: {
			init(id) {
				this.dataForm.id = id || 0
				this.$http({
					url: this.$http.adornUrl('/sys/role/select'),
					method: 'get',
					params: this.$http.adornParams()
				}).then(({
					data
				}) => {
					this.roleList = data && data.code === 0 ? data.list : []
				}).then(() => {
					this.visible = true
					this.$nextTick(() => {
						this.$refs['dataForm'].resetFields()
					})
				}).then(() => {
					if (this.dataForm.id) {
						this.$http({
							url: this.$http.adornUrl(`/testPaper/detail/search?id=${this.dataForm.id}`),
							method: 'get',
							params: this.$http.adornParams()
						}).then(({
							data
						}) => {
							if (data && data.code === 0) {
								this.dataForm.title = data.data.title
								this.dataForm.duration = data.data.duration
								this.dataForm.testMode = data.data.testMode
								this.dataForm.status = data.data.status
								this.dataForm.courseName = data.data.courseName
								this.dataForm.topicNum = data.data.topicNum
								this.$refs.mainId.value = data.data.courseId;
							}
						})
					}
				})
			},
			// 表单提交
			dataFormSubmit() {
				this.$refs['dataForm'].validate((valid) => {
					if (valid) {
						this.$http({
							url: this.$http.adornUrl('/testPaper/save'),
							method: 'post',
							data: this.$http.adornData({
								'id': this.dataForm.id || undefined,
								'title': this.dataForm.title,
								'courseId': this.$refs.mainId.value,
								'duration': this.dataForm.duration,
								'testMode': this.dataForm.testMode,
								'status': this.dataForm.status,
								'courseName': this.dataForm.courseName,
								'topicNum': this.dataForm.topicNum
							})
						}).then(({
							data
						}) => {
							if (data && data.code === 0) {
								this.$message({
									message: '操作成功',
									type: 'success',
									duration: 1500,
									onClose: () => {
										this.visible = false
										this.$emit('refreshDataList')
										this.$parent.getListDate();
									}
								})
							} else {
								this.$message.error(data.msg)
							}
						})
					}
				})
			},
			
			mohuSearch() {
				this.$http({
				  url: this.$http.adornUrl('/course/list'),
				  method: 'get',
				  params: this.$http.adornParams({
					'page': 1,
					'limit': 1000,
					'username': ''
				  })
				}).then(({data}) => {
					const that = this
					this.curricululists = data.page.list
					data.page.list.forEach((v, index) => {
						// 下拉框值进行赋值
						that.searchcuslist.push({ 'value': v.name, 'id': v.id }) // 必须是这种格式
					})
					// this.searchcuslist = res.data
					this.restaurants = this.loadAll()
					this.dataListLoading = false
				})
			},

			querySearch(queryString, cb) {
				var restaurants = this.restaurants;
				var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
				// 调用 callback 返回建议列表的数据
				cb(results);
			},
			createFilter(queryString) {
				return (restaurant) => {
					return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
				};
			},
			loadAll() {
				var that = this
				return that.searchcuslist
			},
			handleSelect(item) {
				this.$refs.mainId.value = item.id;
				console.log(item)
			},
		},
		mounted() {
	//		this.restaurants = this.loadAll();
		}
	}
</script>
